package com.tsj.spzx.product.controller;

import com.github.pagehelper.PageInfo;
import com.tsj.spzx.model.dto.h5.ProductSkuDto;
import com.tsj.spzx.model.dto.product.SkuSaleDto;
import com.tsj.spzx.model.entity.product.ProductSku;
import com.tsj.spzx.model.vo.common.Result;
import com.tsj.spzx.model.vo.common.ResultCodeEnum;
import com.tsj.spzx.model.vo.h5.ProductItemVo;
import com.tsj.spzx.product.service.ProductService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/product")
@Tag(name = "商品接口")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 根据条件 分页查询商品数据
     * @param page
     * @param limit
     * @param productSkuDto
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result findProductByPage(@PathVariable Integer page,
                                    @PathVariable Integer limit,
                                    ProductSkuDto productSkuDto) {
        PageInfo<ProductSku> pageInfo = productService.findProductByPage(page, limit, productSkuDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据skuId查询商品详情
     * @param skuId
     * @return
     */
    @GetMapping("item/{skuId}")
    public Result<ProductItemVo> findProductBySkuId(@PathVariable Long skuId) {
        ProductItemVo productItemVo = productService.findProductBySkuId(skuId);
        return Result.build(productItemVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据skuId查询商品sku信息
     */
    @GetMapping("/getBySkuId/{skuId}")
    public Result<ProductSku> getBySkuId(@PathVariable Long skuId) {
        ProductSku productSku = productService.getBySkuId(skuId);
        return Result.build(productSku, ResultCodeEnum.SUCCESS);
    }

    /**
     * 远程调用
     * 根据skuIdList 查询 商品sku信息
     * @param skuIdList
     * @return
     */
    @PostMapping("/findProductBySkuIdList")
    public List<ProductSku> findProductBySkuIdList(@RequestBody List<Long> skuIdList) {
        List<ProductSku> productSkuList = productService.findProductBySkuIdList(skuIdList);
        return productSkuList;
    }


    /**
     * 远程调用
     * 订单支付完成后，修改商品sku信息
     * @param skuSaleDtoList: 封装的商品skuId 和 销量
     * @return
     */
    @PostMapping("/updateProductSku")
    public Result updateProductSku(@RequestBody List<SkuSaleDto> skuSaleDtoList) {
        productService.updateProductSku(skuSaleDtoList);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
